package com.android.stm;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.timer.ITimerCallback;
import org.anddev.andengine.engine.handler.timer.TimerHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.entity.util.FPSCounter;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.HorizontalAlign;

import android.graphics.Color;
import android.graphics.Typeface;

public class MultiScreen extends BaseGameActivity {
	 
    private static final int CAMERA_WIDTH = 720;
    private static final int CAMERA_HEIGHT = 480;
 
    private Camera mCamera;
    protected Texture mFontTexture;
    protected Font mFont;
 
	private SceneManager sm;
    
    public Engine onLoadEngine() {
            this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
            return new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
    }
 

    public void onLoadResources() {
 
            // Load what's required for the loading screen
            this.mFontTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR);
 
            this.mFont = new Font((BitmapTextureAtlas) this.mFontTexture, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, true, Color.BLACK);
 
            this.getEngine().getTextureManager().loadTexture(this.mFontTexture);
            this.getEngine().getFontManager().loadFont(this.mFont);
            
            this.sm = new SceneManager(this);
    }
 
    
    public Scene onLoadScene() {
            this.getEngine().registerUpdateHandler(new FPSCounter());
 
            // Create the scene we'll be using for the loading screen
            final Scene scene = new Scene();
            scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));
 
            // Add some text
            final Text textCenter = new Text(100, 60, this.mFont, "this is a loading page", HorizontalAlign.CENTER);
            scene.attachChild(textCenter);
            
            scene.registerUpdateHandler(new TimerHandler(2.0f, true, new ITimerCallback() {
                public void onTimePassed(final TimerHandler pTimerHandler) {
                	// Unregister the update handler so it only runs once
                	scene.unregisterUpdateHandler(pTimerHandler);
 
                	//  Load the 3 textures...                	
                	BitmapTextureAtlas texture1 = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
                	BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
                	
                	TextureRegion region1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texture1, MultiScreen.this, "gfx.png", 0, 0);
                	
                	mEngine.getTextureManager().loadTexture(texture1);
                	
                	
                	BitmapTextureAtlas texture2 = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
                	BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
                	
                	TextureRegion region2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texture2, MultiScreen.this, "gfx2.png", 0, 0);
                	
                	mEngine.getTextureManager().loadTexture(texture2);
 
                	
                	MultiScreen.this.sm.loadMain();
                	}
        	}));
 
            return scene;
    }
 
 
    public void onLoadComplete() {
 
    }
}